import csv
from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=12)
filename = '上海天气.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)

    highs,lows,times = [],[],[]
    for row in reader:
        #1去除掉单位转换为数字;2将华氏温度转换为摄氏温度
        high = round((5/9)*(int(row[1][:-1])-32),2)
        low = round((5/9)*(int(row[2][:-1])-32),2)
        highs.append(high)
        lows.append(low)
        times.append(row[0])
    #print(times)
    #根据数据绘制图形
    fig = plt.figure(dpi=128,figsize=(11,5))
    plt.plot(times,highs,c='red',alpha=0.5)
    plt.plot(times,lows,c='blue',alpha=0.5)
    plt.fill_between(times,highs,lows,facecolor='green',alpha=0.1)
    #倾斜横坐标，防止相邻的数据重叠
    fig.autofmt_xdate()
    #设置图形格式
    plt.title("上海2018-08-14天气",fontproperties=font_set)
    plt.xlabel('时间',fontsize=16,fontproperties=font_set)
    plt.ylabel("气温(F)",fontsize=16,fontproperties=font_set)
    plt.tick_params(axis='both',which='major',labelsize=7)
    plt.show()
    #打印标题行
    #print(header_row)
    #for index,column_header in enumerate(header_row):
        #print(index,column_header)
